<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Http\Services\CompanyService;
use App\Models\Company;
use App\Models\CompanyWelfare;
use App\Models\Recruit;
use Illuminate\Http\Request;

class CompanyController extends Controller
{   
    protected $CompanyService;
    public function __construct(CompanyService $CompanyService)
    {
        $this->CompanyService = $CompanyService;
    }

    public function index(Request $Request)
    {
        $search = $Request->search;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Company::when($search, function ($query, $search) {
                    return $query->where('full_name','like', $search.'%');
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }


    public function update(Request $Request)
    {
        // if(!$Request->log){
        //     return response()->json(['code' => '403', 'msg' => '请上传logo']);
        // }
        // if(!$Request->scale){
        //     return response()->json(['code' => '403', 'msg' => '请选择规模']);
        // }
        if(!$Request->full_name){
            return response()->json(['code' => '403', 'msg' => '请输入企业名称']);
        }
        // if(!$Request->introduce){
        //     return response()->json(['code' => '403', 'msg' => '请输入介绍']);
        // }
        // if(!$Request->areas){
        //     return response()->json(['code' => '403', 'msg' => '请选择地区']);
        // }
        // if(!$Request->address){
        //     return response()->json(['code' => '403', 'msg' => '请输入地址']);
        // }
        $input = $Request->input();
        $input['uid'] = $Request->uid;
        $input['eid'] = $Request->eid;
        $data = $this->CompanyService->update($input);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$input]);
    }

    public function info(Request $Request)
    {
        $data = Company::withCount('recruits')->find($Request->id);
        $welfare = CompanyWelfare::where('eid',0)->get();
        foreach ($welfare as $key => $value) {
            $welfare[$key]->name = $value->label;
            $welfare[$key]->value = (string)$value->id;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'welfare'=>$welfare]);
    }

    public function jobList(Request $Request)
    {
        $data = Recruit::where('resource_id',$Request->id)->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function welfare()
    {
        $data = CompanyWelfare::where('eid',0)->get();
        foreach ($data as $key => $value) {
            $data[$key]->name = $value->label;
            $data[$key]->value = (string)$value->id;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

}
